package com.hui.controller;

import com.hui.entity.vo.ProductVo;
import com.hui.entity.vo.ReplyCountVo;
import com.hui.entity.vo.StoreProductReplyQueryVo;
import com.hui.model.storeproduct.StoreProductQueryParam;
import com.hui.service.StoreProductReplyService;
import com.hui.service.StoreProductService;
import com.hui.util.ResponseResult;
import com.hui.util.module.GetUserInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 商品控制层
 */
@RestController
@CrossOrigin
@RequiredArgsConstructor
@Slf4j
@RequestMapping("/api")
public class StoreProductController {

    private final StoreProductService storeProductService;
    private final StoreProductReplyService replyService;

    /**
     * 获取产品列表
     */
    @GetMapping("/products")
    public ResponseResult goodsList(StoreProductQueryParam productQueryParam) {

        return storeProductService.getGoodsListByPage(productQueryParam);
    }

    /**
     * 普通商品详情
     *
     * @param id      商品id
     * @param request 获取token
     * @return
     */
    @GetMapping("/product/detail/{id}")
    public ResponseResult detail(@PathVariable long id, HttpServletRequest request) {

        Long uid = null;
        try {
            uid = GetUserInfo.getUid(request);
        } catch (Exception e) {
            log.info("没有携带token进行商品查询");
        }
        storeProductService.addGoodsBrowseNum(id);//增加一次商品的浏览量

        ProductVo productVo = storeProductService.goodsDetail(id, uid);
        return ResponseResult.okResult(productVo);
    }

    /**
     * 获取产品评论具体数据
     */
    @GetMapping("/reply/list/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品ID", paramType = "query", dataTypeClass = Long.class, dataType = "long", required = true),
            @ApiImplicitParam(name = "type", value = "评论分数类型", paramType = "query", dataTypeClass = Integer.class, dataType = "int"),
            @ApiImplicitParam(name = "page", value = "页码,默认为1", paramType = "query", dataTypeClass = Integer.class, dataType = "int"),
            @ApiImplicitParam(name = "limit", value = "页大小,默认为10", paramType = "query", dataTypeClass = Integer.class, dataType = "int")
    })
    @ApiOperation(value = "获取产品评论", notes = "获取产品评论")
    public ResponseResult<List<StoreProductReplyQueryVo>> replyList(@PathVariable Long id,
                                                                    @RequestParam(value = "type", defaultValue = "0") int type,
                                                                    @RequestParam(value = "page", defaultValue = "1") int page,
                                                                    @RequestParam(value = "limit", defaultValue = "10") int limit) {
        return ResponseResult.okResult(replyService.getReplyList(id, type, page, limit));
    }

    /**
     * 获取产品评论数据总览
     */
    @GetMapping("/reply/config/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品ID", paramType = "query", dataTypeClass = Integer.class, dataType = "int")
    })
    @ApiOperation(value = "获取产品评论数据", notes = "获取产品评论数据")
    public ResponseResult<ReplyCountVo> replyCount(@PathVariable Integer id) {
        return ResponseResult.okResult(replyService.getReplyCount(id));
    }
}
